package com.ydgk.servlet;

import com.ydgk.bean.Order;
import com.ydgk.bean.Pclass;
import com.ydgk.bean.User;
import com.ydgk.service.OrderService;
import com.ydgk.service.PclassService;
import com.ydgk.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet("/OrderServlet.action")
public class OrderServlet extends BaseServlet {

    protected void findOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderform=request.getParameter("orderform");
        String pn=request.getParameter("pageNumber");
        UserService userService=new UserService();
        OrderService orderService=new OrderService();
        List<User> userList=userService.findAll();
        Integer pageNumber = null ;
        try {
            pageNumber = Integer.parseInt(pn);
        }catch (NumberFormatException e){
            pageNumber = 1 ;
        }
        Map map=orderService.AdminSearch(orderform,pageNumber);
        List<Order> orderList=(List<Order>) map.get("orderList");
        String pageCode = (String)map.get("pageCode");
        request.setAttribute("pageCode",pageCode);
        request.setAttribute("orderform",orderform);
        request.setAttribute("userList",userList);
        request.setAttribute("orderList",orderList);
        request.setAttribute("s","2");
        request.getRequestDispatcher("OrderManagement.jsp").forward(request,response);
    }
    protected void updateById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = null ;
        try{
            id=Integer.parseInt(request.getParameter("id"));
        }catch (NumberFormatException e){
        }
        String orderform=request.getParameter("orderform");
        Order order=new Order();
        OrderService orderService=new OrderService();
        order.setId(id);
        order.setOrderform(orderform);
        int i=orderService.updateById(order);
        System.out.println("id="+id);
        if(i>0){
            response.sendRedirect("OrderServlet.action?flag=findOrder");
        }else{
            response.getWriter().print("操作失败");
        }
    }
    protected void deleteById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order order=new Order();
        OrderService orderService=new OrderService();
        String id=request.getParameter("id");
        Integer i=null;
        try {
            i= orderService.deleteById(Integer.parseInt(id));
        }catch (NumberFormatException e){
        }
        PrintWriter out=response.getWriter();
        out.print(i>0?true:false);
    }
    protected void deletes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Order order=new Order();
        OrderService orderService=new OrderService();
        String ids=request.getParameter("ids");
        String[] s=ids.split(",");
        Integer i=null;
        for (String s1 : s) {
            try{
                i=orderService.deleteById(Integer.parseInt(s1));
            }catch (NumberFormatException e) {
            }
            System.out.println(i);
        }
        PrintWriter out=response.getWriter();
        out.print(i>0?true:false);
    }
}
